<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button id="download">下载</button>
</body>
<script>
    document.getElementById('download').onclick = function(){
        // excel的内容,这里的内容一般通过发请求,向后端获取数据
        var csvRows = [['序号','姓名'],['1','张三'],['2','李四']]
        // 生成excel的内容字符串
        var csvString = csvRows.join("\r\n");
        // 创建a标签给它赋值
        var a = document.createElement('a');
        a.href        = 'data:text/csv;charset=utf-8,' + encodeURIComponent(csvString);
        a.target      = '_blank';
        a.download    = 'export.csv';
        // 模拟点击下载excel
        document.body.appendChild(a);
        a.click();
    }
</script>
</html>